### packages
library(cjoint)
library(foreign)
library(dplyr)
library(cregg)
library(ggplot2)
library(patchwork)
library(labelled)

########################
# Load and set up data #
########################
## France
# load recoded data
dat.e1fr <- read.csv("France - cjoint 1.csv")
# organize factor variable order
dat.e1fr$ideology <- factor(dat.e1fr$ideology, levels = c(0,1,2,3,4))
dat.e1fr$leadexp <- factor(dat.e1fr$leadexp, levels = c("Media","Business","Politics"))
dat.e1fr$selection <- factor(dat.e1fr$selection, levels = c("Leadership","Delegates","Members","All voters"))
dat.e1fr$status <- factor(dat.e1fr$status, levels = c("New party","Opposition","Government"))
dat.e1fr$women <- factor(dat.e1fr$women, levels = c("Below average","Above average"))

## Israel
## load recoded data 
dat.e1il <- read.csv("Israel - cjoint 1.csv")
## organize factor variable order
dat.e1il$ideology <- factor(dat.e1il$ideology, levels = c(0,1,2,3,4))
dat.e1il$leadexp <- factor(dat.e1il$leadexp, levels = c("Media","Business","Politics"))
dat.e1il$selection <- factor(dat.e1il$selection, levels = c("Leadership","Delegates","Members","All voters"))
dat.e1il$status <- factor(dat.e1il$status, levels = c("New party","Opposition","Government"))
dat.e1il$women <- factor(dat.e1il$women, levels = c("Below average","Above average"))

## Sweden
## load recoded data 
dat.e1sw <- read.csv("Sweden - cjoint 1.csv")
## organize factor variable order
dat.e1sw$ideology <- factor(dat.e1sw$ideology, levels = c(0,1,2,3,4))
dat.e1sw$leadexp <- factor(dat.e1sw$leadexp, levels = c("Media","Business","Politics"))
dat.e1sw$selection <- factor(dat.e1sw$selection, levels = c("Leadership","Delegates","Members","All voters"))
dat.e1sw$status <- factor(dat.e1sw$status, levels = c("New party","Opposition","Government"))
dat.e1sw$women <- factor(dat.e1sw$women, levels = c("Below average","Above average"))


#####################################
# AMCE by profile order (Figure B1) #
#####################################
## generate factor variable for profile order
dat.e1fr$profile_fac <- factor(dat.e1fr$profile)
dat.e1il$profile_fac <- factor(dat.e1il$profile)
dat.e1sw$profile_fac <- factor(dat.e1sw$profile)
## perform estimates by profile order - AMCE
out.amce.fr1.prfle <- cj(data = dat.e1fr,
                   selected ~ ideology + selection + leadexp + status + women,
                   id = ~Response.ID,
                   estimate = "amce",
                   by = ~profile_fac,
)
out.amce.il1.prfle <- cj(data = dat.e1il,
                   selected ~ ideology + selection + leadexp + status + women,
                   id = ~Response.ID,
                   estimate = "amce",
                   by = ~profile_fac,
)
out.amce.sw1.prfle <- cj(data = dat.e1sw,
                   selected ~ ideology + selection + leadexp + status + women,
                   id = ~Response.ID,
                   estimate = "amce",
                   by = ~profile_fac,
)
## plot outcomes
# define attribute order for graphs
out.amce.fr1.prfle$feature <- factor(out.amce.fr1.prfle$feature, 
                               levels = c("selection","ideology","leadexp","status","women"))
out.amce.il1.prfle$feature <- factor(out.amce.il1.prfle$feature, 
                               levels = c("selection","ideology","leadexp","status","women"))
out.amce.sw1.prfle$feature <- factor(out.amce.sw1.prfle$feature, 
                               levels = c("selection","ideology","leadexp","status","women"))
# define labels for graphs
attributes <- c("Ideological \nDistance","Candidae \nSelection","Leader \nExperience","Party \nStatus",
                "Female \nRepresentation") 
names(attributes) <- c("ideology","selection","leadexp","status","women")
# plot graphs
plot.amce.fr1.prfle <- plot(out.amce.fr1.prfle, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Party A", "Party B")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank()) +
  ggplot2::ggtitle("France") 
plot.amce.il1.prfle  <- plot(out.amce.il1.prfle, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Party A", "Party B")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank(), axis.text.y=element_blank()) +
  ggplot2::ggtitle("Israel")
plot.amce.sw1.prfle  <- plot(out.amce.sw1.prfle, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Party A", "Party B")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank(), axis.text.y=element_blank()) +
  ggplot2::ggtitle("Sweden")
plot.acme.all1.prfle  <- plot.amce.fr1.prfle  + plot.amce.il1.prfle  + plot.amce.sw1.prfle  + 
  plot_layout(guides = "collect") & theme(legend.position = "bottom")
# save file (Figure B1)
ggsave(file = "FigureB1.pdf", plot=plot.acme.all1.prfle, width=8, height=6.5)

##################################
# AMCE by task order (Figure B2) #
##################################
## generate factor variable for profile order
dat.e1fr$task_fac <- factor(dat.e1fr$task)
dat.e1il$task_fac <- factor(dat.e1il$task)
dat.e1sw$task_fac <- factor(dat.e1sw$task)
## perform estimates by task order - AMCE
out.amce.fr1.tsk <- cj(data = dat.e1fr,
                         selected ~ ideology + selection + leadexp + status + women,
                         id = ~Response.ID,
                         estimate = "amce",
                         by = ~task_fac,
)
out.amce.il1.tsk <- cj(data = dat.e1il,
                         selected ~ ideology + selection + leadexp + status + women,
                         id = ~Response.ID,
                         estimate = "amce",
                         by = ~task_fac,
)
out.amce.sw1.tsk <- cj(data = dat.e1sw,
                         selected ~ ideology + selection + leadexp + status + women,
                         id = ~Response.ID,
                         estimate = "amce",
                         by = ~task_fac,
)
## plot outcomes
# define attribute order for graphs
out.amce.fr1.tsk$feature <- factor(out.amce.fr1.tsk$feature, 
                                     levels = c("selection","ideology","leadexp","status","women"))
out.amce.il1.tsk$feature <- factor(out.amce.il1.tsk$feature, 
                                     levels = c("selection","ideology","leadexp","status","women"))
out.amce.sw1.tsk$feature <- factor(out.amce.sw1.tsk$feature, 
                                     levels = c("selection","ideology","leadexp","status","women"))
# define labels for graphs
attributes <- c("Ideological \nDistance","Candidae \nSelection","Leader \nExperience","Party \nStatus",
                "Female \nRepresentation") 
names(attributes) <- c("ideology","selection","leadexp","status","women")
# plot graphs
plot.amce.fr1.tsk <- plot(out.amce.fr1.tsk, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Task 1", "Task 2", "Task 3")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank()) +
  ggplot2::ggtitle("France") 
plot.amce.il1.tsk  <- plot(out.amce.il1.tsk, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Task 1", "Task 2", "Task 3")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank(), axis.text.y=element_blank()) +
  ggplot2::ggtitle("Israel")
plot.amce.sw1.tsk  <- plot(out.amce.sw1.tsk, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_discrete(labels = c("Task 1", "Task 2", "Task 3")) +
  ggplot2::xlim(-0.6,0.3) +
  ggplot2::theme(legend.title = element_blank(), axis.text.y=element_blank()) +
  ggplot2::ggtitle("Sweden")
plot.acme.all1.tsk  <- plot.amce.fr1.tsk  + plot.amce.il1.tsk  + plot.amce.sw1.tsk  + 
  plot_layout(guides = "collect") & theme(legend.position = "bottom")
# save file (Figure B2)
ggsave(file = "FigureB2.pdf", plot=plot.acme.all1.tsk, width=8, height=6.5)

